/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.db;

import framework.config.Database;
import framework.data.Quarto;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Thairony
 */
public class QuartoManager
{
    private static QuartoManager instance;

    /**
     * 
     * @return 
     */
    public static QuartoManager getInstance()
    {
        if ( instance == null )
        {
            instance = new QuartoManager();
        }

        return instance;
    }
    
    /**
     * 
     * @return
     * @throws Exception 
     */
    private Database getDatabase() throws Exception
    {
        return Database.getInstance();
    }
    
    /**
     * 
     * @param quarto
     * @throws Exception 
     */
    public void addQuarto( Quarto quarto ) throws Exception
    {
        Database db = getDatabase();
        
        quarto.setCodigo( db.getNextId( "quarto" ) );
        
        try
        {
            String sql = "insert into quarto (" + 
                    " codigo, " +
                    " numero, " +
                    " andar, " +
                    " cd_grupo,  " +
                    " cd_produto " +
                    " ) values ( " +
                    " ?, ?, ?, ?, ? )";

            PreparedStatement ps = db.prepareStatement( sql );
            
            int i = 1;
            ps.setInt( i++, quarto.getCodigo() );
            ps.setInt( i++, quarto.getNumero() );
            ps.setInt( i++, quarto.getAndar() );
            ps.setInt( i++, quarto.getGrupo() );
            ps.setInt( i++, 0 );
            
            ps.execute();
        }
        
        finally
        {
            db.close();
        }           
    }
    
    /**
     * 
     * @param quarto
     * @throws Exception 
     */
    public void updateQuarto( Quarto quarto ) throws Exception
    {
        Database db = getDatabase();
        
        try
        {
            String sql = "update quarto set " + 
                    " numero = ?," +
                    " andar = ?," +
                    " grupo = ? " +
                    " where " +
                    " codigo = ? ";

            PreparedStatement ps = db.prepareStatement( sql );
            
            int i = 1;
            ps.setInt( i++, quarto.getNumero() );
            ps.setInt( i++, quarto.getAndar() );
            ps.setInt( i++, quarto.getGrupo() );
            ps.setInt( i++, quarto.getCodigo() );
            
            ps.execute();
        }
        
        finally
        {
            db.close();
        }           
    }
    
    /**
     * 
     * @param p
     * @throws Exception 
     */
    public void deleteQuarto( Quarto p ) throws Exception
    {
        Database db = getDatabase();
        
        try
        {
            String sql = "delete from quarto where codigo = ?";
            
            PreparedStatement ps = db.prepareStatement( sql );
            
            ps.setInt( 1, p.getCodigo() );
            
            ps.execute();
        }
        
        finally
        {
            db.close();
        }
    }
    
    /**
     * 
     * @return
     * @throws Exception 
     */
    public List<Quarto> getQuarto() throws Exception
    {
        Database db = getDatabase();
        
        try
        {
            String sql = Selects.SELECT_QUARTO;
            
            PreparedStatement ps = db.prepareStatement( sql );
            
            return fetchQuartos( ps );
        }
        
        finally
        {
            db.close();
        }
    }
    
    /**
     * 
     * @param codigo
     * @return
     * @throws Exception 
     */
    public Quarto getQuarto( int codigo ) throws Exception
    {
        Database db = getDatabase();
        
        try
        {
            String sql = Selects.SELECT_QUARTO +
                    " where " +
                    " codigo = ?";
            
            PreparedStatement ps = db.prepareStatement( sql );
            
            ps.setInt( 1, codigo );
            
            return fetchQuarto( ps );
        }
        
        finally
        {
            db.close();
        }
    }
    
    /**
     * 
     * @param st
     * @return
     * @throws Exception 
     */
    private List<Quarto> fetchQuartos( PreparedStatement st ) throws Exception
    {
        ResultSet rs = st.executeQuery();
        
        List<Quarto> usuarios = new ArrayList<Quarto>();
        
        while ( rs.next() )
        {
            Quarto quarto = new Quarto();
        
            int i = 1;
            quarto.setCodigo( rs.getInt( i++ ) );
            quarto.setNumero( rs.getInt( i++ ) );
            quarto.setAndar( rs.getInt( i++ ) );
            quarto.setGrupo( rs.getInt( i++ ) );

            usuarios.add( quarto );
        }
        
        return usuarios;
    }
    
    /**
     * 
     * @param st
     * @return
     * @throws Exception 
     */
    private Quarto fetchQuarto( PreparedStatement st ) throws Exception
    {
        ResultSet rs = st.executeQuery();
        
        Quarto quarto = null;
        
        if ( rs.next() )
        {
            quarto = new Quarto();
            
            int i = 1;
            quarto.setCodigo( rs.getInt( i++ ) );
            quarto.setNumero( rs.getInt( i++ ) );
            quarto.setAndar( rs.getInt( i++ ) );
            quarto.setGrupo( rs.getInt( i++ ) );
        }
        
        return quarto;
    }
}
